/*
 * Copyright (C) 2007 IsmAvatar <cmagicj@nni.com>
 * 
 * This file is part of Conflict Map Editor (CME).
 * CME is free software and comes with ABSOLUTELY NO WARRANTY.
 * See LICENSE for details.
 */

package org.conflict.main;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;

import javax.swing.JPanel;

/**
 * A panel that displays some tiles for the user to choose from.
 * The user can choose a Left and Right button tile, and they will be highlighted.
 * get/setLeft and get/setRight methods determine which tile ID is chosen with which button.
 * getImage obtains the image associated with that ID.
 */
public class TileChooser extends JPanel
	{
	private static final long serialVersionUID = 1L;
	private int left;
	private int right;
	private String files;
	private int height;
	private BufferedImage[] tile;

	/**
	 * Constructs a tile chooser panel
	 * @param files Path to obtain tile icons from, relative to "icons/". Include the final slash.
	 * @param height How many tiles shall be shown vertically. There will always be 3 horizontally.
	 * @param left The initial selection for the left mouse button. Use 0 for none.
	 */
	public TileChooser(String files, int height, int left)
		{
		this.files = files;
		this.height = height;
		tile = new BufferedImage[height * 3 + 1];
		this.left = left;
		setOpaque(true);
		enableEvents(MouseEvent.MOUSE_PRESSED);
		}

	public void paintComponent(Graphics g)
		{
		super.paintComponent(g);
		for (int y = 0; y < height; y++)
			for (int x = 0; x < 3; x++)
				g.drawImage(getImage(y * 3 + x + 1),x * MapEditor.TILE_WIDTH,y * MapEditor.TILE_HEIGHT,null);
		paintSelection(g);
		}

	/**
	 * Draws the Green/Red rectangle around the currently selected tiles.
	 * You should never have to call this directly, since setLeft, setRight, and updateUI call it. 
	 * I considered using XOR mode, but decided against it since it doesn't work well with Gray.
	 */
	public void paintSelection(Graphics g)
		{
		//		g.setXORMode(Color.BLACK);
		g.setColor(Color.GREEN);
		if (left != 0)
			{
			g.drawRect((left - 1) % 3 * MapEditor.TILE_WIDTH,(left - 1) / 3 * MapEditor.TILE_HEIGHT,
					MapEditor.TILE_WIDTH,MapEditor.TILE_HEIGHT);
			g.drawRect((left - 1) % 3 * MapEditor.TILE_WIDTH + 1,(left - 1) / 3 * MapEditor.TILE_HEIGHT
					+ 1,MapEditor.TILE_WIDTH - 2,MapEditor.TILE_HEIGHT - 2);
			g.drawRect((left - 1) % 3 * MapEditor.TILE_WIDTH + 2,(left - 1) / 3 * MapEditor.TILE_HEIGHT
					+ 2,MapEditor.TILE_WIDTH - 4,MapEditor.TILE_HEIGHT - 4);
			}
		g.setColor(Color.RED);
		if (right != 0)
			{
			g.drawRect((right - 1) % 3 * MapEditor.TILE_WIDTH + 3,(right - 1) / 3 * MapEditor.TILE_HEIGHT
					+ 3,MapEditor.TILE_WIDTH - 6,MapEditor.TILE_HEIGHT - 6);
			g.drawRect((right - 1) % 3 * MapEditor.TILE_WIDTH + 4,(right - 1) / 3 * MapEditor.TILE_HEIGHT
					+ 4,MapEditor.TILE_WIDTH - 8,MapEditor.TILE_HEIGHT - 8);
			g.drawRect((right - 1) % 3 * MapEditor.TILE_WIDTH + 5,(right - 1) / 3 * MapEditor.TILE_HEIGHT
					+ 5,MapEditor.TILE_WIDTH - 10,MapEditor.TILE_HEIGHT - 10);
			}
		g.setPaintMode();
		}

	/** Returns a lazily-cached copy of the tile image corresponding to given tile ID */
	public BufferedImage getImage(int p)
		{
		if (tile[p] != null) return tile[p];
		tile[p] = MapEditor.getImage(files + p + ".gif");
		return tile[p];
		}

	public int getLeft()
		{
		return left;
		}

	/**
	 * Sets the tile that gets placed when the left button is pressed.
	 * For your convenience, this updates the graphic to reflect this change.
	 */
	public void setLeft(int left)
		{
		this.left = left;
		updateUI();
		}

	public int getRight()
		{
		return right;
		}

	/**
	 * Sets the tile that gets placed when the left button is pressed
	 * For your convenience, this updates the graphic to reflect this change.
	 */
	public void setRight(int right)
		{
		this.right = right;
		updateUI();
		}

	@Override
	protected void processMouseEvent(MouseEvent e)
		{
		super.processMouseEvent(e);
		if (e.getX() >= getPreferredSize().width && e.getY() >= getPreferredSize().height) return;
		if (e.getID() == MouseEvent.MOUSE_PRESSED && e.getButton() == MouseEvent.BUTTON1)
			{
			setLeft((e.getY() / MapEditor.TILE_HEIGHT) * 3 + (e.getX() / MapEditor.TILE_WIDTH) + 1);
			return;
			}
		if (e.getID() == MouseEvent.MOUSE_PRESSED && e.getButton() == MouseEvent.BUTTON3)
			{
			setRight((e.getY() / MapEditor.TILE_HEIGHT) * 3 + (e.getX() / MapEditor.TILE_WIDTH) + 1);
			return;
			}
		}
	}
